package com.cinema.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cinema.Bean.Hall;
import com.cinema.dao.IHallDao;
import com.cinema.dao.imp.HallDaoMysql;

public class HallServlet extends HttpServlet {
    
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	private IHallDao iHallDao = new HallDaoMysql();
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String method = request.getParameter("method");
		// 场厅新增
		if ("add".equals(method)) {
			String hallId = request.getParameter("hallId");
			String hallName = request.getParameter("hallName");
			
			Hall h = new Hall();
			h.setHallId(hallId);
			h.setHallName(hallName);

			int insert = iHallDao.insert(h);

			if (insert == 1) {
				
				request.getSession().removeAttribute("hlist");
			}
			response.sendRedirect("Hall/index.jsp");
		} else if("delete".equals(method)) {
			String hallId = request.getParameter("hallId");
			if (hallId != null && hallId != "") {
				
				Hall hall = new Hall();
				hall.setHallId(hallId);

				int delete = iHallDao.delete(hall);

				if (delete == 1) {
					response.getWriter().print("success"); 
					request.getSession().removeAttribute("hlist");
				}else{
					
					response.getWriter().print("error");
				}
				
			}
		}else if("getHall".equals(method)){
			String hallId = request.getParameter("hallId");
			
			if(hallId!=null && hallId!=""){
				Hall hall = iHallDao.SelectById(hallId);
				request.setAttribute("hall", hall);
			}
			request.getRequestDispatcher("/Hall/edit.jsp").forward(request, response);
			
		}else if("update".equals(method)){
			String hallId = request.getParameter("hallId");
			String hallName = request.getParameter("hallName");
			
			Hall hall=new Hall(hallId,hallName);
			int update = iHallDao.update(hall);
			if(update==1){
				request.getSession().removeAttribute("hlist");
			}
			response.sendRedirect("Hall/index.jsp");
		}else if("select".equals(method)){
			request.getSession().removeAttribute("hlist");
			String keyword=request.getParameter("keyword");
			
			List<Hall> hall=iHallDao.Select(keyword);
			request.getSession().setAttribute("hlist", hall);
		}
	}

}
